package com.ruoyi.web.controller.deviceManager;

import com.cs.deviceManager.domain.DevDeviceCommand;
import com.cs.deviceManager.service.IDevDeviceCommandService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import java.util.List;

/**
 * 设备指令管理Controller
 * 
 * @author zizige
 * @date 2025-06-30
 */
@Controller
@RequestMapping("/deviceManager/command")
public class DevDeviceCommandController extends BaseController
{
    private String prefix = "deviceManager/command";

    @Resource
    private IDevDeviceCommandService devDeviceCommandService;

    @RequiresPermissions("deviceManager:command:view")
    @GetMapping()
    public String command()
    {
        return prefix + "/command";
    }

    /**
     * 查询设备指令列表
     */
    @RequiresPermissions("deviceManager:command:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DevDeviceCommand devDeviceCommand)
    {
        startPage();
        List<DevDeviceCommand> list = devDeviceCommandService.selectDevDeviceCommandList(devDeviceCommand);
        return getDataTable(list);
    }

    /**
     * 新增设备指令
     */
    @RequiresPermissions("deviceManager:command:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存设备指令
     */
    @RequiresPermissions("deviceManager:command:add")
    @Log(title = "设备指令", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DevDeviceCommand devDeviceCommand)
    {
        return toAjax(devDeviceCommandService.insertDevDeviceCommand(devDeviceCommand));
    }

    /**
     * 修改设备指令
     */
    @RequiresPermissions("deviceManager:command:edit")
    @GetMapping("/edit/{commandId}")
    public String edit(@PathVariable("commandId") Long commandId, ModelMap mmap)
    {
        DevDeviceCommand devDeviceCommand = devDeviceCommandService.selectDevDeviceCommandByCommandId(commandId);
        mmap.put("devDeviceCommand", devDeviceCommand);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备指令
     */
    @RequiresPermissions("deviceManager:command:edit")
    @Log(title = "设备指令", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DevDeviceCommand devDeviceCommand)
    {
        return toAjax(devDeviceCommandService.updateDevDeviceCommand(devDeviceCommand));
    }

    /**
     * 删除设备指令
     */
    @RequiresPermissions("deviceManager:command:remove")
    @Log(title = "设备指令", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String[] ids)
    {
        return toAjax(devDeviceCommandService.deleteDevDeviceCommandByCommandIds(ids));
    }

    /**
     * 发送设备指令
     */
    @RequiresPermissions("deviceManager:command:send")
    @Log(title = "设备指令发送", businessType = BusinessType.OTHER)
    @PostMapping("/send")
    @ResponseBody
    public AjaxResult sendCommand(@RequestParam Long commandId)
    {
        DevDeviceCommand command = devDeviceCommandService.selectDevDeviceCommandByCommandId(commandId);
        if (command == null) {
            return AjaxResult.error("指令不存在");
        }
        boolean result = devDeviceCommandService.sendCommandToDevice(command);
        if (result) {
            return AjaxResult.success("指令发送成功");
        } else {
            return AjaxResult.error("指令发送失败");
        }
    }

    /**
     * 查询设备待执行指令
     */
    @RequiresPermissions("deviceManager:command:list")
    @PostMapping("/pending")
    @ResponseBody
    public TableDataInfo getPendingCommands(@RequestParam String deviceId)
    {
        startPage();
        List<DevDeviceCommand> list = devDeviceCommandService.getPendingCommands(deviceId);
        return getDataTable(list);
    }

    /**
     * 查看设备指令详情
     */
    @RequiresPermissions("deviceManager:command:view")
    @GetMapping("/detail/{commandId}")
    public String detail(@PathVariable("commandId") Long commandId, ModelMap mmap)
    {
        DevDeviceCommand devDeviceCommand = devDeviceCommandService.selectDevDeviceCommandByCommandId(commandId);
        mmap.put("devDeviceCommand", devDeviceCommand);
        return prefix + "/detail";
    }
}